﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections.ObjectModel;

namespace Veracruz.IdentityModel
{
    /// <summary>
    /// Identity claims used by Veracruz
    /// </summary>
    public class IdentityClaimDefinitions
    {
        #region const
        public const String IdentityClaimPrefix = "http://identity.veracruz.org/claims";
        public const String IdentityClaimVersion = "2008/07";
        public const String IdentityClaimFormat = "{0}/{1}/{2}";
        #endregion

        #region Claims
        public static readonly string DisplayNameClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "DisplayName");
        public static readonly string UniqueIdentifierClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "uid");
        public static readonly string DelegationTokenClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "DelegationToken");
        public static readonly string SecurityTokenClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "SecurityToken");
        public static readonly string AuthenticationSchemeClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "AuthenticationScheme");

        public static readonly string FirstNameClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "FirstName");
        public static readonly string LastNameClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "LastName");
        public static readonly string AuthenticationTokenClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "AuthenticationToken");
        public static readonly string SessionKeyClaim = string.Format(IdentityClaimDefinitions.IdentityClaimFormat, IdentityClaimDefinitions.IdentityClaimPrefix, IdentityClaimDefinitions.IdentityClaimVersion, "SessionKey");

        #endregion

        #region Helpers
		public static ICollection<string> GetAllClaims()
        {
            ICollection<String> __userClaims = new Collection<String>();
            MemberInfo[] __fieldInfo = typeof(IdentityClaimDefinitions).GetMembers(BindingFlags.Public | BindingFlags.Static);
            IEnumerable<MemberInfo> __claims = __fieldInfo.Where(c => c.Name.EndsWith("Claim") && c.MemberType == MemberTypes.Field);
            return __claims.ToList().ConvertAll<string>(delegate(MemberInfo member) { return member.ReflectedType.GetField(member.Name).GetValue(null) as String; });
        }

        public static bool IsMatchingClaim(string claimValue)
        {
            return claimValue.StartsWith(IdentityClaimDefinitions.IdentityClaimPrefix);
        }
    	#endregion    
    }
}
